package vn.axonivy.vision.nashorn;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.time.LocalDateTime;

import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

public class NashornExamples {

	public static void main(String args[]) throws ScriptException, NoSuchMethodException, FileNotFoundException{
		ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
		engine.eval(new FileReader(NashornExamples.class.getResource("InvokingJavascript.js").getPath()));

		Invocable invocable = (Invocable) engine;
		
		invocable.invokeFunction("printGreetings", "AA");
		
		Object result = invocable.invokeFunction("getGreetings", "AA");
		System.out.println(result);
		
		System.out.println(result.getClass());
		
		invocable.invokeFunction("printClass", LocalDateTime.now());
		
		engine.eval(new FileReader(NashornExamples.class.getResource("InvokingJava.js").getPath()));
	}

	
	public static void printJavaGreetings(String name) {
	    System.out.println("Hi there from Java, " + name);
	}
	
	public static String getJavaGreetings(String name) {
	    return "greetings from Java";
	}
}
